﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BabyFood.Data;
using BabyFood.Entities;
using System.Data;
using System.Data.SqlClient;
using System.Xml;
using System.Configuration;
using System.Drawing;
using System.IO;

public partial class dct_Video_QLVideo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Pager1.CurrentIndex = 1;
            Bind_Data(Pager1.CurrentIndex);
        }
    }

    //public string Uploadedfilename { get { return Convert.ToString(ViewState["filename"]); } set { ViewState["filename"] = value; } }

    protected void Bind_Data(int PageNo)
    {
        int TotalRows = 0;
        string WhereClause = "1=1";
        TList<Video> TList_LienKet = DataRepository.VideoProvider.GetPaged(WhereClause, "NgayCapNhat DESC", PageNo - 1, Pager1.PageSize, out TotalRows);
        gridCatalog.DataSource = TList_LienKet;
        gridCatalog.DataBind();

        Pager1.ItemCount = TotalRows;
        if (TotalRows <= Pager1.PageSize)
            Pager1.Visible = false;
        else
            Pager1.Visible = true;
    }

    protected void Pager1_Command(object sender, CommandEventArgs e)
    {
        try
        {
            int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
            Pager1.CurrentIndex = currnetPageIndex;
            Bind_Data(currnetPageIndex);
        }
        catch (Exception ex)
        {
            Common.WriteLog("QLVideo.aspx.cs", "Pager1_Command()", ex.ToString());
        }
    }

    protected void cmdSave_Click(object sender, ImageClickEventArgs e)
    {
        bool updateStatus = false;
        try
        {
            Video entVideo = new Video();
            entVideo.Id = Convert.ToInt32(H_CatalogId.Value); ;
            entVideo.Ten = Common.StandardString(txtTen.Text);
            entVideo.DienGiai = Server.HtmlEncode(txtContent.Text);
            entVideo.Code = txtVideo.Text;
            entVideo.TomTat = txtTomTat.Text;
            //entVideo.Code = Common.StandardString(H_Video.Value);
            entVideo.TrangThai= chkTinNong.Checked;
            entVideo.NgayCapNhat = Common.GetServerDateTime();
            if (entVideo.Id == 0)
            {
                updateStatus = DataRepository.VideoProvider.Insert(entVideo);
            }
            else
            {
                entVideo.EntityState = EntityState.Changed;
                updateStatus = DataRepository.VideoProvider.Update(entVideo);
            }
            if (updateStatus)
            {
                Bind_Data(Pager1.CurrentIndex);
                cmdReset_Click(sender, e);
            }
            else
                WebMessage.Show("Cập nhật Video thất bại!");
        }
        catch (Exception ex)
        {
            Common.WriteLog("QLVideo.aspx.cs", "cmdSave_Click", ex.ToString());
            WebMessage.Show("Cập nhật Video thất bại!");
        }
    }


    protected void cmdReset_Click(object sender, ImageClickEventArgs e)
    {
        H_CatalogId.Value = "0";
        txtTen.Text = "";
        txtContent.Text = "";
        txtVideo.Text = "";
        //H_Video.Value = "";
        chkTinNong.Checked = true;
    }

    protected void gridCatalog_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //if (e.Row.RowType == DataControlRowType.DataRow)
        //{
        //    LinkButton l = (LinkButton)e.Row.FindControl("cmdDelete");
        //    l.Attributes.Add("onclick", "javascript:return " + "confirm('Bạn chắc chắn muốn xóa thông tin này?');");

        //    e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.backgroundColor='WhiteSmoke';";
        //    e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='White';";
        //}
    }

    protected void gridCatalog_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Select")
        {
            try
            {
                H_CatalogId.Value = e.CommandArgument.ToString();
                Video entVideo = DataRepository.VideoProvider.GetById(Convert.ToInt32(H_CatalogId.Value));
                if (entVideo != null)
                {
                    txtTen.Text = entVideo.Ten;
                    txtContent.Text = Server.HtmlDecode(entVideo.DienGiai);
                    txtTomTat.Text = Common.StandardString(txtTomTat.Text);
                    //H_Video.Value = entVideo.Code;
                    txtVideo.Text = entVideo.Code;
                    chkTinNong.Checked = Convert.ToBoolean(entVideo.TrangThai);
                }
            }
            catch (Exception ex)
            {
                Common.WriteLog("QLVideo.aspx.cs", "gridCatalog_RowCommand - Select", ex.ToString());
            }
        }
        if (e.CommandName == "Delete")
        {
            try
            {
                int IdVideo = Convert.ToInt32(e.CommandArgument.ToString());
                bool deleteStatus = DataRepository.VideoProvider.Delete(IdVideo);
                if (!deleteStatus)
                {
                    WebMessage.Show("Xóa Video thất bại");
                }
            }
            catch (Exception)
            {
                WebMessage.Show("Xóa Video thất bại");
            }
        }
    }

    protected void gridCatalog_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        H_CatalogId.Value = "0";
        txtTen.Text = "";
        txtContent.Text = "";
        txtVideo.Text = "";
        txtTomTat.Text = "";
        //H_Video.Value = "";
        chkTinNong.Checked = true;
        Bind_Data(Pager1.CurrentIndex);
    }
}